<?php

namespace app\index\controller;

use app\index\controller\User;
use think\facade\Config;
use think\facade\Session;

/**
 *  网络监测
 */
class Quality extends User
{    
    public $token = '';
    public function initialize()
    {
        parent::initialize();
        $this->token = session("login_token");       
        if(!$this->token){
            $this->redirect("index/user/login");
        }
    }


    /**
     * 网络监测（展示页面）
     */
    public function info(){
        $this->view->assign('title', __('网络监测')); 
        return $this->view->fetch();
    }

    /**
     * ping 基础链路
     */
    public function basedlink()
    {
        $propertyname = input('propertyname');
        $res = $this->curl($propertyname);
        $name = '基础链路';
        $data = $this->handle($res,$name);
        die(json_encode($data,JSON_UNESCAPED_UNICODE));
    }

    /**
     * webpage 网页
     */
    public function webpage()
    {
        $propertyname = input('propertyname');
        $res = $this->curl($propertyname);
        $name = '网页/元素';
        $data = $this->handle($res,$name);
        die(json_encode($data,JSON_UNESCAPED_UNICODE));
    }



    /**
     * webpage 视频质量
    */
    public function video()
    {
        $propertyname = input('propertyname');
        $res = $this->curl($propertyname);
        $name = '视频';
        $data = $this->handle($res,$name);
        die(json_encode($data,JSON_UNESCAPED_UNICODE));
    }


    /**
     * webpage 游戏质量
    */
    public function game()
    {
        $propertyname = input('propertyname');
        $res = $this->curl($propertyname);
        $name = '游戏';
        $data = $this->handle($res,$name);
        die(json_encode($data,JSON_UNESCAPED_UNICODE));
    }


    /**
     * filedownload 下载速率
     */
    public function filedownload()
    {
        $propertyname = input('propertyname');
        $res = $this->curl($propertyname);
        $name = '文件下载';
        $data = $this->handle($res,$name);
        die(json_encode($data,JSON_UNESCAPED_UNICODE));
    }

    /**
     * 请求接口 
     *
     * @param      <type>  $type   The type
     *
     * @return     <type>  ( description_of_the_return_value )
     */
    public function curl($propertyname){
        $data = array(
            'propertyname' => $propertyname,
            'token' => $this->token,
            'terminal' => 'web',
            'gradename' => '5分钟',
            'starttime' => date("Y-m-d H:i:s",strtotime("-1 day")),
            'endtime' => date("Y-m-d H:i:s",time()),
        );
        $data_string = json_encode($data,JSON_UNESCAPED_UNICODE);
        // dump($data_string);die;
        $curlopt = array(
            CURLOPT_HTTPHEADER=>array ('Content-Type: application/json; charset=utf-8', 'Content-Length: ' . strlen ( $data_string ) ),
            CURLOPT_TIMEOUT=>Config::get("site.api_timeout"),
        );
        $url = request()->domain().'/api/nqm/link_export';
        // echo $url;die;
        // dump($curlopt);die;
        // dump($data_string);die;
        $res = curl_post($url,$data_string,$curlopt);
        $res = json_decode($res,true);
        return $res;
    }


    //统一的方法处理链路质量数据
    public function handle($res,$name)
    {
        $arr['code'] = $res['code'];
        $arr['msg'] = $res['msg'];
        $arr['xAxis'] = [];
        $arr['series'] = [];
        if($res['code'] == 1){            
            $item = array();         
            $data = $res['data'];
            $str = array();
            foreach($data as $k=>$v){
                $str[] = $v['item'];
                // foreach($v['item'] as $k1=>$v1){
                //     $str = $v1;
                //     $date = array_unique(array_column($v1,'timeInterval'));
                // }
            }
            // dump($str);die;
            foreach($str as $k=>$v){
                foreach($v as $k1=>$v1){
                    $item[] = $v1['timeInterval'];
                }
            }
            // die;
            $date = array_unique($item);
            $date = array_values($date);
            sort($date);
            foreach($date as $k=>$v){
                $ks[] = $k;
            }
            // $arr_result = [];
            // foreach ($data as $key => $value) {
            //     // dump($value);
            //     foreach($value['item'] as $k=>$v){
            //         $qualityScore[$key][] = $v['qualityScore'];
            //     }
            // }
            

            $arr_result = [];
            foreach ($data as $key => $value) {
                foreach($value['item'] as $k=>$v){
                    if($value['export'] == count($date)){
                        $ks[] = $k;
                        // $qualityScore[$key][] = $v['qualityScore'];
                    }

                    if($value['export'] < count($date)){                        
                        $keys[] = array_search($v['timeInterval'], $date);
                        $values[$key][$v['timeInterval']] =   $v['qualityScore'];                                        
                    }

                }
            }

            foreach($data as $key => $value){
                foreach($value['item'] as $k=>$v){
                    if($value['export'] == count($date)){
                        // dump($v['qualityScore']);
                        $qualityScore[$key][] = $v['qualityScore'];                        
                    }
                    if($value['export'] < count($date)){
                        foreach($ks as $k1=>$v1){
                            $qualityScore[$key][$v1] = null;                            
                        }

                        foreach($values as $k2=>$v2){
                            foreach($v2 as $k3=>$v3){
                                $k4 = array_search($k3,$date);
                                // dump($k4);
                                $qualityScore[$k2][$k4] = $v3;
                            }                            
                        }
                        // sort($qualityScore);
                        break;                  
                    }
                }
            }
            // die;
            // sort($qualityScore);          
            // dump($qualityScore);die;
            foreach($data as $key=>$value){
                if(count($value['item'])>0){
                    $arr_result[$key] = ['name'=>$value['export'].'出口','data'=>$qualityScore[$key]];
                }
            }
            // die;
            // dump($arr_result);die;
            // $qualityScore = array_column($data,'qualityScore');
            $arr['xAxis'] = $date;
            $arr['series'] = array_values($arr_result);
        }
        return $arr;
    }




}
